// pages/index/index.js
const nav = require('../../require/nav');
const ComFunJS = require('../../common/ComFunJS');
const app = getApp();
const navSrc = `${app.globalData.request}/api/base/cate/listall/1?ctype=`;      //nav
var page = '1';
var page_size = 5;
var sort = "last";
var is_easy = 0;
var lange_id = 0;
var pos_id = 0;
var unlearn = 0;
var url = '';
// 请求数据
var loadMore = function (that) {
  that.setData({
    hidden: false
  });
  wx.request({
    url: app.globalData.request + "/api/pro/list/" + that.data.first + "/" + that.data.second + "?pageindex=" + page,
    success: function (res) {
      var list = that.data.list;
      for (var i = 0; i < res.data.result.length; i++) {
        res.data.result[i].ImgUrl = ComFunJS.FilePath(res.data.result[i].ImgUrl,'/135/120');
        list.push(res.data.result[i])
      } 
      that.setData({
        list: list
      });     
      that.setData({
        hidden: true
      });
    }
  });
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classList: [],
    hidden: true,
    list: [],
    scrollTop: 0,
    scrollHeight: 0,
    ctype:"1",
    first:"1",    
    second:"",
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {   
    var _this = this;
     wx.getSystemInfo({
      success:function(res) {
        _this.setData({
              scrollHeight:res.windowHeight
        });      
      }
       });    
   
    wx.request({
      url: `${app.globalData.request}/api/base/cate/listall/1?ctype=1&topid=`,
      method: 'GET',
      success: function (res) {
        _this.setData({
          classList: res.data.result,
          first:res.data.result[0].ID,
          second: res.data.result[0].SubItem[0].ID
        });
        loadMore(_this);
      },
      fail: function (res) {
        // fail
      },
      complete: function (res) {
        // complete
      }
    })
  },
  list: function (event) {
    console.log(event)
    if (event.currentTarget.dataset.elType.ID != this.data.first) {
      this.setData({
       first : event.currentTarget.dataset.elType.ID,
       second: event.currentTarget.dataset.elType.SubItem[0].ID
      })
    }
    if (event.currentTarget.dataset.elCode!=='') {
      this.setData({
        second: event.currentTarget.dataset.elCode.ID,
        list:[]
      })
      page='1';
      this.SubTitle = event.currentTarget.dataset.elCode.TypeName;
      loadMore(this);
    }
  },  
  getlist:function(){
    this.setData({
      first: first,
      second:second
    })
  },
  //页面滑动到底部
  onscroll: function () {
    var that = this;
    page++;
    loadMore(that);
  },
  topLoad:function(event) {
         //   该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
    this.setData({
      scrollTop : 0
      });
          loadMore(this);
    
  }  ,
  scroll: function (event) {
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
      scrollTop: event.detail.scrollTop
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})